﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElectronicDiary.Models;
using ElectronicDiary.Models.Parent;

namespace ElectronicDiary.Controllers
{
    [Authorize(Roles = "Parent")]
    public class ParentController : Controller
    {
        //
        // GET: /Parent/
        
        public ActionResult Panel()
        {
            using (var context = new electronicDiaryEntities())
            {
                int parentid = context.parent.SingleOrDefault(n => n.aspnet_Membership.aspnet_Users.UserName == User.Identity.Name).idParent;

                ChildViewModel[] chvm = context.student
                    .Where(n => n.idParent == parentid)
                    .OrderBy(n => n.lastName.Trim())
                    .Select(n => new ChildViewModel { ID = n.idStudent, Name = n.firstName.Trim() + " " + n.lastName.Trim() }).ToArray();

                return View(chvm);
            }
        }

        public ActionResult Oceny(int id)
        {
            using (var context = new electronicDiaryEntities())
            {
                GradesViewModel gvm = new GradesViewModel();
                var st = context.student.SingleOrDefault(n=>n.idStudent == id);
                gvm.Student = st.firstName.Trim() + " " + st.lastName.Trim();

                int MaxCol = context.grade
                    .Where(n => n.idStudent == id)
                    .GroupBy(n => n.idSubject)
                    .Select(n => n.Count())
                    .Max();

                gvm.Columns = new ColumnViewModel[2]{ 
                    new ColumnViewModel{Name="Przedmiot"}, 
                    new ColumnViewModel{Name="Oceny", Type="O", ColSpan = MaxCol}};

                var subjects = context.grade
                    .Where(n => n.idStudent == id)
                    .GroupBy(n => n.idSubject)
                    .Select(n=> new 
                        {
                            IloscO = n.Count(),
                            Przediot = n.Select(m=>m.subject.name.Trim()).FirstOrDefault(),
                            IdP = n.Select(m=>m.idSubject).FirstOrDefault()
                        })
                    .OrderBy(n=>n.Przediot);

                RowViewModel[] rvm = new RowViewModel[subjects.Count()];

                for (int i = 0; i < subjects.Count(); i++)
                {
                    //int k = ;
                    CellValueViewModel[] cvvm = new CellValueViewModel[subjects.Skip(i).First().IloscO + 1];
                    cvvm[0] = new CellValueViewModel { Value = subjects.Skip(i).First().Przediot };

                    IEnumerable<CellValueViewModel> grades = context.grade
                        .Where(n => n.idSubject == subjects.Skip(i).FirstOrDefault().IdP && n.idStudent == id)
                        .Select(n => new CellValueViewModel
                        {
                            Value = n.grade1,
                            ToolTip = n.notes.Trim()
                        }).ToArray();

                    for (int j = 1; j < grades.Count() + 1; j++)
                    {                       
                        cvvm[j] = new CellValueViewModel 
                        {
                            Value = grades.Skip(j - 1).FirstOrDefault().Value == null ? "" : grades.Skip(j - 1).FirstOrDefault().Value,
                            ToolTip = grades.Skip(j - 1).FirstOrDefault().ToolTip == null ? "" : grades.Skip(j - 1).FirstOrDefault().ToolTip, 
                        };
                    }
                    rvm[i] = new RowViewModel { Values = cvvm };
                }

                gvm.Rows = rvm;

                return View(gvm);
            }
        }
    }
}
